//
//  AddPlanTableViewCell.m
//  MobileOffice
//
//  Created by jjkj on 16/11/14.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "AddPlanTableViewCell.h"

@implementation AddPlanTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _startTitle=[MyControl creatLabelWithFrame:CGRectMake(10, 10, 80, 30) text:@"出        发："];
        _startTitle.textColor=[UIColor grayColor];
        _startTitle.layer.masksToBounds=YES;
        _startTitle.layer.cornerRadius=5;
        _startTitle.font=FONT_14;
        _startTextField=[[CustomField alloc]initWithFrame:CGRectMake(_startTitle.x+_startTitle.width+5, _startTitle.y, IPHONE_WIDTH-(_startTitle.x+_startTitle.width+5)-10, _startTitle.height)];
        //_startTextField.color=COLOR_BACKGROUND_GRAY;
        //_startTextField.textAlignment=NSTextAlignmentCenter;
        _startTextField.delegate=self;
        _startTextField.tag=101;
        _startTextField.font=FONT_14;
        [self.contentView addSubview:_startTitle];
        [self.contentView addSubview:_startTextField];
        
        _endTitle=[MyControl creatLabelWithFrame:CGRectMake(_startTitle.x, _startTitle.y+_startTitle.height+10, 80, 30) text:@"目        的："];
        _endTitle.textColor=[UIColor grayColor];
        _endTitle.layer.masksToBounds=YES;
        _endTitle.layer.cornerRadius=5;
        _endTitle.font=FONT_14;
        _endTextField=[[CustomField alloc]initWithFrame:CGRectMake(_endTitle.x+_endTitle.width+5, _endTitle.y, _startTextField.width, _startTitle.height)];
        //_endTextField.color=COLOR_BACKGROUND_GRAY;
        //_endTextField.textAlignment=NSTextAlignmentCenter;
        _endTextField.delegate=self;
        _endTextField.tag=102;
        _endTextField.font=FONT_14;
        [self.contentView addSubview:_endTitle];
        [self.contentView addSubview:_endTextField];
        
        _meansTitle=[MyControl creatLabelWithFrame:CGRectMake(10, _endTitle.y+_endTitle.height+10, 80, 30) text:@"出行方式："];
        _meansTitle.textColor=[UIColor grayColor];
        _meansTitle.layer.masksToBounds=YES;
        _meansTitle.layer.cornerRadius=5;
        _meansTitle.font=FONT_14;
        _meansTextField=[[CustomField alloc]initWithFrame:CGRectMake(_startTitle.x+_startTitle.width+5, _meansTitle.y, IPHONE_WIDTH/2-(_startTitle.x+_startTitle.width+5)-10, _startTitle.height)];
       // _meansTextField.color=COLOR_BACKGROUND_GRAY;
        _meansTextField.textAlignment=NSTextAlignmentCenter;
        _meansTextField.delegate=self;
        _meansTextField.tag=103;
        _meansTextField.font=FONT_14;
        [self.contentView addSubview:_meansTitle];
        [self.contentView addSubview:_meansTextField];
        
        _spendTitle=[MyControl creatLabelWithFrame:CGRectMake(IPHONE_WIDTH/2+10, _meansTitle.y, 80, 30) text:@"费用预计："];
        _spendTitle.textColor=[UIColor grayColor];
        _spendTitle.layer.masksToBounds=YES;
        _spendTitle.layer.cornerRadius=5;
        _spendTitle.font=FONT_14;
        _spendTextField=[[CustomField alloc]initWithFrame:CGRectMake(_spendTitle.x+_spendTitle.width+5, _meansTitle.y, IPHONE_WIDTH/2-(_startTitle.x+_startTitle.width+20+5)-10, _startTitle.height)];        //_spendTextField.color=COLOR_BACKGROUND_GRAY;
        _spendTextField.textAlignment=NSTextAlignmentCenter;
        _spendTextField.delegate=self;
        _spendTextField.keyboardType=UIKeyboardTypeNumberPad;
        _spendTextField.tag=104;
        _spendTextField.font=FONT_14;
        [self.contentView addSubview:_spendTitle];
        [self.contentView addSubview:_spendTextField];
        
        UILabel *spendTitle2=[MyControl creatLabelWithFrame:CGRectMake(_spendTextField.x+_spendTextField.width, _meansTitle.y, 20, 30) text:@"元"];
        spendTitle2.textColor=[UIColor grayColor];
        spendTitle2.font=FONT_14;
        [self.contentView addSubview:spendTitle2];
        
        //添加分割线
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 129, IPHONE_WIDTH, 1)];
        line.backgroundColor=BACKCOLOR_GRAY;
        [self.contentView addSubview:line];
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (_spendTextField.tag==104) {
        if (string.length == 0) return YES;
        
//        NSInteger existedLength = textField.text.length;
//        NSInteger selectedLength = range.length;
//        NSInteger replaceLength = string.length;
        //第一个不允许输入小数点
        if (textField.text.length==0&&[string isEqualToString:@"."]) {
            return NO;
        }
        //不允许出啊先两个小数点
        if ([textField.text containsString:@"."]&&[string isEqualToString:@"."]) {
            return NO;
        }
//        NSInteger count=2;
//        //有小数点则限制为4为  整数为2位
//        if ([textField.text containsString:@"."]||[string isEqualToString:@"."]) {
//            count=4;
//        }
//        if (existedLength - selectedLength + replaceLength > count) {
//            return NO;
//        }
        
        return YES;
    }
    return YES;
}
#pragma mark -textFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(CustomField *)textField{
    if (_appearBlock) {
        _appearBlock();
    }
    //取消红色边框
    [self setLabelBorderColorWithLabel:self.startTitle color:[UIColor clearColor]];
    [self setLabelBorderColorWithLabel:self.endTitle color:[UIColor clearColor]];
    [self setLabelBorderColorWithLabel:self.meansTitle color:[UIColor clearColor]];
    [self setLabelBorderColorWithLabel:self.spendTitle color:[UIColor clearColor]];
    
    //选择公出交通类型
    if (textField.tag==103) {
        [self endEditing:YES];
        NSMutableArray *titleArr=[NSMutableArray new];
//            [titleArr addObject:SELECTPERSONTEXT];
        for (DutyDropListModel *model in _dropListModel.dataArr) {
            [titleArr addObject:model.keyword];
        }
        //[UIView beginAnimations:nil context:nil];
        
        //[UIView setAnimationDuration:2];
        DepartPersonListView *departPersonView=[[DepartPersonListView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) titleArr:titleArr];
        [[[UIApplication sharedApplication].delegate window] addSubview:departPersonView];
//        [UIView commitAnimations];


        __weak AddPlanTableViewCell *myCell=self;
        departPersonView.myBlock=^(NSString *departName){
            myCell.meansTextField.text=departName;
            myCell.planModel.meansStr=[departName copy];
            myCell.meansTextField.textColor=[UIColor blackColor];
            for (DutyDropListModel *model in myCell.dropListModel.dataArr) {
                if ([model.keyword isEqualToString:departName]) {
                    myCell.planModel.keywordId=[model.keywordId copy];
                    break;
                }
            }
        };
        return NO;
    }
    textField.color=COLOR_SKYBLUE;
    return YES;
}

- (void)setTextViewWithModel:(AddPlanModel *)model{
    _startTextField.text=model.startStr;
    _endTextField.text=model.endStr;
    _meansTextField.text=model.meansStr;
    _spendTextField.text=model.spendStr;
    _planModel=model;
}

- (BOOL)textFieldShouldEndEditing:(CustomField *)textField{
    if (_disAppearBlock) {
        _disAppearBlock();
    }
    textField.color=[UIColor lightGrayColor];
    if (textField.tag==101) {
        self.planModel.startStr=[textField.text copy];
    }
    if (textField.tag==102) {
        self.planModel.endStr=[textField.text copy];
    }
    if (textField.tag==104) {
        self.planModel.spendStr=[textField.text copy];
    }
    return YES;
}

#pragma mark -设置label边框
- (void)setLabelBorderColorWithLabel:(UILabel *)label color:(UIColor *)color{
    label.layer.borderColor=[color CGColor];
    label.layer.borderWidth=1.0;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
